package com.cn.client;

import com.cn.common.core.codc.ConstantValue;
import com.cn.common.core.model.Request;
import com.cn.common.module.ModuleId;
import com.cn.common.module.login.UserCmd;
import com.cn.common.module.login.request.LoginRequest;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class MainTest {

    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 10102);
            OutputStream outputStream = socket.getOutputStream();
            LoginRequest loginRequest = new LoginRequest();
            loginRequest.setLoginName("123");
            loginRequest.setToken("xtoken-123");

            //构建请求
            Request request = Request.valueOf(ModuleId.USER, UserCmd.LOGIN, loginRequest.getBytes());

            ByteBuf buffer = Unpooled.buffer();
            //包头
            buffer.writeInt(ConstantValue.HEADER_FLAG);
            //module
            buffer.writeShort(request.getModule());
            //cmd
            buffer.writeShort(request.getCmd());
            //长度
            int lenth = request.getData() == null ? 0 : request.getData().length;
            if (lenth <= 0) {
                buffer.writeInt(lenth);
            } else {
                buffer.writeInt(lenth);
                buffer.writeBytes(request.getData());
            }
            outputStream.write(buffer.array());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
